/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.kmagsoft.jconvert.utils;

import com.kmagsoft.jconvert.gui.ApplicationParams;
import com.kmagsoft.jconvert.gui.BatchConvertGui;
import gr.anagnosg.general.utils.ExtrernalUtils;
import gr.anagnosg.general.utils.FileUtils;
import gr.anagnosg.general.utils.PropertiesUtils;
import gr.anagnosg.image.imagemagick.Constants;
import gr.anagnosg.image.imagemagick.ParameterList;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.JComboBox;
import javax.swing.ListModel;

/**
 *
 * @author anagnosg
 */
public class GuiUtils {
    
    
    
    public static JComboBox loadSelectListAppFile(JComboBox jcombo,  String optionsSelect){
        jcombo.removeAllItems();
        FileReader input = null;
        try {
           input = new FileReader("params.properties"); //pplicationParams.class.getResourceAsStream("../application.properties");// getResourceAsStream("application.properties");
        } catch (FileNotFoundException ex) {
           Logger.getLogger(BatchConvertGui.class.getName()).log(Level.SEVERE, null, ex);
        }
        Properties prop = PropertiesUtils.loadProperties(input,false);
        
        Enumeration en = prop.keys();
        
        while(en.hasMoreElements()){
            String key = (String)en.nextElement();
            if(key.startsWith(optionsSelect)){
                String valueT = prop.getProperty(key);
                jcombo.addItem(valueT); 
            }
        }
        return jcombo;
    }
    
    
     public static List findActionParams(String action ){
        List output = new ArrayList();
        FileReader input = null;
        try {
           input = new FileReader("params.properties"); //pplicationParams.class.getResourceAsStream("../application.properties");// getResourceAsStream("application.properties");
        } catch (FileNotFoundException ex) {
           Logger.getLogger(BatchConvertGui.class.getName()).log(Level.SEVERE, null, ex);
        }
        Properties prop = PropertiesUtils.loadProperties(input,false);
        
        Enumeration en = prop.keys();
        
        while(en.hasMoreElements()){
            String key = (String)en.nextElement();
            if(key.startsWith(action)){
                String valueT = prop.getProperty(key);
                output.add(valueT);
            }
        }
        return output;
    }
    
    /*public static void addOptions2List(javax.swing.JList jlist,String newValue){
    
        DefaultListModel model = (DefaultListModel) jlist.getModel();
        model.addElement(newValue);

        
    }*/
    
     
     public static void saveParamProperyFile(LinkedHashMap  model){
         String propF = "params.properties";
         PropertiesUtils.clearProperies(propF);
        

        for (Object key : model.keySet()) {
            String action = key.toString();
            List params = (List)model.get(action);
            for(int i = 0 ; i<params.size() ; i ++){
                String param =(String)params.get(i);
                PropertiesUtils.saveProperty(propF, action+"_"+i, param);
            }
            
        }
         
         
     }
     
     public static String splitCommand(String parentDest, String output,String convertCommand,List<String> mergedFilenames,ParameterList params) throws IOException, InterruptedException{
         //split an 10.
         //mergedFilenames.add(0, convertCommand); 
         //mergedFilenames.add(mergedFilenames.size(), parentDest+File.separatorChar+output); 
         List<String> partListFile = new ArrayList<String>();
         String outputExt = FileUtils.getExtension(output);
         int until=mergedFilenames.size()+1;
         boolean run =true;
         boolean last=false;
         String commandoutput="";
         int j = 0 ;
         String tempFilenameUnique="";
         String tempFilenameUniquePrevious="";
         List<String> paramsForMerge = null;
         if(params!=null){
            paramsForMerge=params.commandStringListMergable();
         }
         while(run){
            partListFile.add(convertCommand);
            if(paramsForMerge!=null){
                partListFile.addAll(paramsForMerge);
            }
             /*if(FileUtils.isTiff(output)){
                 partListFile.add(" -append ");
                 System.out.println("tiff found ");
                 
             }*/
             //tempFilenameUniquePrevious
             if(j>0){
                partListFile.add(tempFilenameUniquePrevious);
             }
             tempFilenameUnique=parentDest+File.separatorChar+ new Date().getTime()+"."+outputExt;
             for(int i = 0 ; i<until;i++){
                 partListFile.add(mergedFilenames.get(j));
                 j++;
                 if(j>=mergedFilenames.size()){
                     run=false;
                     last=true;
                     break;
                 }
             }
             if(last){
                partListFile.add(output);
             }
             else{
                partListFile.add(tempFilenameUnique);
             }
             tempFilenameUniquePrevious=tempFilenameUnique;
             
             commandoutput = ExtrernalUtils.executeCommandBuilder(partListFile);
             partListFile.clear();
         }
         return commandoutput;
     }
}
